﻿using System;
using FizzWare.NBuilder;
using FizzWare.NBuilder.Dates;
using FizzWare.NBuilder.Generators;
using NHDay_Linq.Domain;
using NHDay_Linq.Helper;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Linq;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using System.Linq;
namespace NHDay_Linq
{
	[TestFixture]
	public class SimpleTests : TestBase
	{
        [Test, Explicit("This creates the structure of the database")]
        public void SchemaExport()
        {
            CreateDatabaseSchema();
        }

        [SetUp]
        public void SetUp()
        {
            using (ISession session = SessionFactory.OpenSession())
            {
                CreateDatabaseSchema();
               //session.CreateQuery("delete from HeldItem").ExecuteUpdate();
               //session.CreateQuery("delete from Person").ExecuteUpdate();
            }
        }

	    [Test]
        public void TestOperatorOnDateTime()
        {
            using (ISession session = SessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                DateTime start = The.Year(2010).On.April.The1st;
                DateTime middle = start.AddDays(5);
                DateTimeGenerator generator = new DateTimeGenerator(start);
                var persons = Builder<Adult>.CreateListOfSize(10)
                    .WhereAll().Have(x => x.Id = 0)
                    .WhereAll().Have(x => x.BirthDate = generator.NextDay())
                    .Build();
                foreach (var person in persons)
                {
                    session.Save(person);
                }
                session.Flush();
                var result = session.Query<Person>()
                    .Where(p => p.BirthDate > middle)
                    .Count();
                Assert.That(result, Is.EqualTo(5));

                 result = session.Query<Person>()
                   .Where(p => p.BirthDate > start.AddDays(5))
                   .Count();
                Assert.That(result, Is.EqualTo(5));

                result = session.Query<Person>()
                  .Where(p => p.BirthDate > new DateTime(2010, 4, 6))
                  .Count();
                Assert.That(result, Is.EqualTo(5));

                result = session.Query<Person>()
                 .Where(p => p.BirthDate > The.Year(2010).On.April.The6th)
                 .Count();
                Assert.That(result, Is.EqualTo(5));

				result = session.Query<Person>()
			   .Where(p => p.BirthDate.Year == 2010)
			   .Count();
				Assert.That(result, Is.EqualTo(10));

            }
        }

        [Test]
        public void TestTypeOfElement()
        {
            using (ISession session = SessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
               
                var adults = Builder<Adult>.CreateListOfSize(7)
                    .WhereAll().Have(x => x.Id = 0)
                    .Build();
                foreach (var person in adults)
                {
                    session.Save(person);
                }
                var childs = Builder<Child>.CreateListOfSize(13)
                     .WhereAll().Have(x => x.Id = 0)
                     .Build();
                foreach (var person in childs)
                {
                    session.Save(person);
                }
                session.Flush();
				var result1 = session.Query<Person>()
					.Where(p => p is Adult)
					.Count();
				Assert.That(result1, Is.EqualTo(7));
               
                var result2 = session.Query<Person>()
                  .Where(p => p is Child)
                  .Count();
                Assert.That(result2, Is.EqualTo(13));
               
            }
        }
	}
}
